using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class CardView : MonoBehaviour
{
    [SerializeField] private TMP_Text title;
    [SerializeField] private TMP_Text description;
    [SerializeField] private TMP_Text mana;
    [SerializeField] private SpriteRenderer imageRenderer;
    [SerializeField] private GameObject wrapper;
    [SerializeField] private LayerMask dropLayer;

    private Vector3 dragStartPosition;
    private Quaternion dragStartRotation;

    public Card Card { get; private set; }

    public void SetUp(Card card)
    {
        this. Card = card;
        title.text = card.Title;
        description.text = card.Description;
        mana.text = card.Mana.ToString();
        imageRenderer.sprite = card.Image;
    }
    private void OnMouseEnter()
    {
        if (!Interactions.instance.PlayerCanHover()) return;
        wrapper.SetActive(false);
        Vector3 pos=new Vector3(transform.position.x,-2,0);
        CardViewHoverSystem.instance.Show(Card,pos);    
    }
    private void OnMouseExit()
    {
        if (!Interactions.instance.PlayerCanHover()) return;
        CardViewHoverSystem.instance.Hide();
       wrapper.SetActive(true);
    }
    private void OnMouseDown()
    {
        if (!Interactions.instance.PlayerCanInteract()) return;
        if(Card.ManualTargetEffect!= null)
        {
            ManualTargetSystem.instance.StartTargeting(transform.position);
        }
        else
        {
            Interactions.instance.PlayerIsDragging= true;
            wrapper.SetActive(true);
            CardViewHoverSystem.instance.Hide();
            dragStartPosition = transform.position;
            dragStartRotation = transform.rotation;
            transform.rotation = Quaternion.Euler(0, 0, 0);
            transform.position=MouseUtil.GetMousePositionInWorldSpace(-1);
        }
    }
    private void OnMouseDrag()
    {
        if (!Interactions.instance.PlayerCanInteract()) return;
        if (Card.ManualTargetEffect != null) return;
        transform.position=MouseUtil.GetMousePositionInWorldSpace(-1);
    }
    private void OnMouseUp()
    {
        if (!Interactions.instance.PlayerCanInteract()) return;
        if (Card.ManualTargetEffect != null)
        {
            EnemyView target= ManualTargetSystem.instance.EndTargeting(MouseUtil.GetMousePositionInWorldSpace(-1));
    
            if (target != null&&ManaSystem.instance.HasEnoughMana(Card.Mana))
            {
                Debug.Log(target.gameObject.name);
                PlayCardGA playCardGA= new(Card,target);
                ActionSystem.instance.Perform(playCardGA);
            }
        }
        else
        {
            if (ManaSystem.instance.HasEnoughMana(Card.Mana)&&
                Physics.Raycast(transform.position, Vector3.forward, out RaycastHit hit, 10, dropLayer))
            {
                PlayCardGA playrardGA = new(Card);
                ActionSystem.instance.Perform(playrardGA);
            }
            else
            {
                transform.position = dragStartPosition;
                transform.rotation = dragStartRotation;
            }
            Interactions.instance.PlayerIsDragging = false;
        }
    }

}
